package xyz.yuanjilu.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import xyz.yuanjilu.annotation.SysLog;
import xyz.yuanjilu.base.AntInfo;
import xyz.yuanjilu.service.WebAntManagerService;
import xyz.yuanjilu.utils.PageUtils;
import xyz.yuanjilu.utils.Query;
import xyz.yuanjilu.utils.R;

/**
 * 
 * 
 * @author chenjunfei
 * @email kkomge@qq.com
 * @date 2017-07-18 14:56:39
 * 爬虫管理列表
 */
@RestController
@RequestMapping("webantmanager")
public class WebAntManagerController extends WebMagicAbstractController{
    
    @Autowired
    private WebAntManagerService webAntManager;
    
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("webantmanager:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<AntInfo> antInfos = webAntManager.queryList(query);
        int total = webAntManager.queryTotal(query);
        
        PageUtils pageUtil = new PageUtils(antInfos, total, query.getLimit(), query.getPage());
        
        return R.ok().put("page", pageUtil);
    }
    
    /**
     * 启动
     */
    @SysLog("启动爬虫")
    @RequestMapping("/start")
    public R start(@RequestBody String[] ids){
        webAntManager.start(ids);
        return R.ok();
    }
    /**
     * 停止
     */
    @SysLog("停止爬虫")
    @RequestMapping("/stop")
    public R stop(@RequestBody String[] ids){
        webAntManager.stop(ids);
        return R.ok();
    }
    
    /**
     * 添加定时任务
     */
    @SysLog("添加爬虫定时任务")
    @RequestMapping("/addScheduleJob")
    public R addScheduleJob(@RequestBody String[] ids){
        webAntManager.addScheduleJob(ids);
        return R.ok();
    }
    /**
     * 添加定时任务
     */
    @SysLog("启动爬虫定时任务")
    @RequestMapping("/startScheduleJob")
    public R startScheduleJob(@RequestBody String[] ids){
        webAntManager.startScheduleJob(ids);
        return R.ok();
    }
    /**
     * 添加定时任务
     */
    @SysLog("停止爬虫定时任务")
    @RequestMapping("/stopScheduleJob")
    public R stopScheduleJob(@RequestBody String[] ids){
        webAntManager.stopScheduleJob(ids);
        return R.ok();
    }
    /**
     * 绑定站点,
     */
    @SysLog("爬虫绑定站点")
    @RequestMapping("/bindwebsite")
    public R bindwebsite(@RequestBody BindwebsiteForm bindwebsiteForm){
        System.err.println(bindwebsiteForm);
        webAntManager.bindwebsite(bindwebsiteForm.getIds(), bindwebsiteForm.getSelectWebId());
        return R.ok();
    }
}
class BindwebsiteForm{
    String[] ids;
    String selectWebId;
 
    public String[] getIds() {
        return ids;
    }
    public void setIds(String[] ids) {
        this.ids = ids;
    }
    public String getSelectWebId() {
        return selectWebId;
    }
    public void setSelectWebId(String selectWebId) {
        this.selectWebId = selectWebId;
    }
    
    
}